﻿Namespace Functions
    Public Class MetisFunctionFieldList
        Inherits System.ComponentModel.BindingList(Of MetisFunctionField)

        Private _OwningFunction As MetisFunction

        ''' <summary>Creates a new instance of a function fieldlist</summary>
        ''' <param name="OwningFunction">The parent function owning this fields</param>
        Sub New(ByVal OwningFunction As MetisFunction)
            _OwningFunction = OwningFunction
        End Sub

        ''' <summary>Gets or sets the element at the specified index.</summary>
        ''' <param name="index">The zero-based index of the element to get or set.</param>
        Default Public Shadows Property Item(ByVal index As Integer) As MetisFunctionField
            Get
                Return MyBase.Item(index)
            End Get
            Set(ByVal value As MetisFunctionField)
                MyBase.Item(index) = value
            End Set
        End Property

        ''' <summary>Gets or sets the element with the specified name</summary>
        ''' <param name="FieldName">The fieldname of the element to get or set.</param>
        ''' <exception cref="IndexOutOfRangeException">Thrown if the specified FieldName is not found.</exception>
        Default Public Shadows Property Item(ByVal FieldName As String) As MetisFunctionField
            Get
                For Each tmpItem As MetisFunctionField In Me
                    If tmpItem.Name.Equals(FieldName, StringComparison.OrdinalIgnoreCase) Then
                        Return tmpItem
                    End If
                Next
                Throw New IndexOutOfRangeException(String.Format("'{0}' is not an element of the list.", FieldName))
            End Get
            Set(ByVal value As MetisFunctionField)
                For Each tmpItem As MetisFunctionField In Me
                    If tmpItem.Name.Equals(FieldName, StringComparison.OrdinalIgnoreCase) Then
                        tmpItem = value
                        Exit Property
                    End If
                Next
                Throw New IndexOutOfRangeException(String.Format("'{0}' is not an element of the list.", FieldName))
            End Set
        End Property

        ''' <summary>Checks if the specified fieldname is in the fieldlist</summary>
        ''' <param name="FieldName">The fieldname to look for.</param>
        ''' <returns>true if a field with that name is in the list, false if not</returns>
        Public Overloads Function Contains(ByVal FieldName As String) As Boolean
            Try
                Return Me(FieldName) IsNot Nothing
            Catch ex As IndexOutOfRangeException
                Return False
            End Try
        End Function

        ''' <summary>Checks if the specified ID is in the fieldlist</summary>
        ''' <param name="ID">The ID to look for.</param>
        ''' <returns>true if a field with that ID is in the list, false if not</returns>
        Public Overloads Function Contains(ByVal ID As Integer) As Boolean
            For Each Item As MetisFunctionField In Me
                If Item.ID = ID Then
                    Return True
                End If
            Next
            Return False
        End Function

        ''' <summary>Checks if the specified DB fieldname is used in the fieldlist</summary>
        ''' <param name="DBFieldName">The DB fieldname to look for.</param>
        ''' <returns>true if a field with that name is in the list, false if not</returns>
        Public Overloads Function ContainsDBField(ByVal DBFieldName As String) As Boolean
            For Each Item As MetisFunctionField In Me
                If Item.DBFieldName.Equals(DBFieldName, StringComparison.OrdinalIgnoreCase) Then
                    Return True
                End If
            Next
            Return False
        End Function

        ''' <summary>Gets an element by its ID</summary>
        ''' <param name="ID">The DB-ID</param>
        ''' <returns>The field with the specified ID</returns>
        ''' <exception cref="IndexOutOfRangeException">Thrown if the ID is not found.</exception>
        Public Function FieldByID(ByVal ID As Integer) As MetisFunctionField
            For Each Item As MetisFunctionField In Me
                If Item.ID = ID Then
                    Return Item
                End If
            Next
            Throw New IndexOutOfRangeException(String.Format("There is no element with the ID '{0}' within this list.", ID))
        End Function
    End Class
End Namespace

